"""
@File    :   path.py
@Time    :   2024/01/07 20:00:35
@Author  :   Qrj
@Email   :   923207736@qq.com
@description   :   路径工具类
"""

from functools import lru_cache
from pathlib import Path
from typing import Iterator


class PathUtil:
    """路径工具类"""

    @staticmethod
    @lru_cache
    def get_project_root_path() -> Path:
        """获取项目的根目录路径

        Returns:
            Path: 路径对象
        """
        return Path(__file__).parent.parent.parent.resolve()

    @staticmethod
    @lru_cache
    def get_project_env_path() -> Path:
        """获取.env文件的路径

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_project_root_path().joinpath(".env")

    @staticmethod
    @lru_cache
    def get_business_path() -> Path:
        """获取业务代码的文件夹路径

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_project_root_path().joinpath("src", "business")

    @staticmethod
    @lru_cache
    def get_business_admin_path() -> Path:
        """获取后台部分的业务代码的文件夹路径

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_business_path().joinpath("admin")

    @staticmethod
    @lru_cache
    def get_business_h5_path() -> Path:
        """获取前台页面部分的业务代码的文件夹路径

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_business_path().joinpath("h5")

    @staticmethod
    @lru_cache
    def get_business_applet_path() -> Path:
        """获取小程序部分的业务代码的文件夹路径

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_business_path().joinpath("applet")

    @staticmethod
    @lru_cache
    def get_encryption_path() -> Path:
        """获取加密文件夹路径

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_project_root_path().joinpath("encryption")

    @staticmethod
    @lru_cache
    def get_rsa_encryption_path() -> Path:
        """获取Rsa公钥与私钥存放的文件夹路径

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_encryption_path().joinpath("rsa")

    @staticmethod
    @lru_cache
    def get_rsa_public_key_file_path() -> Path:
        """获取Rsa公钥文件位置

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_rsa_encryption_path().joinpath("public.pub")

    @staticmethod
    @lru_cache
    def get_rsa_private_key_file_path() -> Path:
        """获取Rsa私钥文件位置

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_rsa_encryption_path().joinpath("private.pem")

    @staticmethod
    @lru_cache
    def get_alembic_ini_path() -> Path:
        """获取alembic配置文件路径

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_project_root_path().joinpath("alembic.ini")

    @staticmethod
    @lru_cache
    def get_alembic_dir_path() -> Path:
        """获取alembic文件夹路径

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_project_root_path().joinpath("alembic")

    @staticmethod
    @lru_cache
    def get_alembic_versions_dir_path() -> Path:
        """获取alembic的versions文件夹路径

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_alembic_dir_path().joinpath("versions")

    @staticmethod
    @lru_cache
    def get_page_template_dir_path() -> Path:
        """获取页面模板文件夹路径

        Returns:
            Path: 路径对象
        """
        return PathUtil.get_project_root_path().joinpath("templates")

    @staticmethod
    @lru_cache
    def get_upload_dir_path() -> Path:
        """获取upload文件夹路径"""
        return PathUtil.get_project_root_path().joinpath("upload")

    @staticmethod
    @lru_cache
    def get_static_dir_path() -> Path:
        """获取static文件夹路径"""
        return PathUtil.get_project_root_path().joinpath("static")

    @staticmethod
    @lru_cache
    def get_static_captcha_dir_path() -> Path:
        """获取captcha静态文件文件夹路径"""
        return PathUtil.get_static_dir_path().joinpath("captcha")

    @staticmethod
    @lru_cache
    def get_static_sliding_puzzle_captcha_dir_path() -> Path:
        """获取滑块验证码静态文件文件夹路径"""
        return PathUtil.get_static_captcha_dir_path().joinpath("sliding_puzzle")

    @staticmethod
    def rglob_pathlib(
        base_path: Path,
        joinpath_: str | list[str] | None = None,
        regexp_str: str = "*",
    ) -> Iterator[Path]:
        """递归遍历指定文件夹

        Args:
            base_path (Path | str): 基础路径
            joinpath (str | list[str] | None, optional): 连接路径
            regexp_str (str, optional): 正则

        Yields:
            Iterator[Path]: 返回Path对象的生成器
        """
        path = base_path
        # 拼接路径
        if joinpath_:
            if isinstance(joinpath_, str):
                path = path.joinpath(joinpath_)
            else:
                path = path.joinpath(*joinpath_)
        return path.rglob(regexp_str)

    @staticmethod
    def get_excluded_suffix_filename(path: str | Path) -> str:
        """获取去除文件扩展名后的文件名称

        Args:
            path (Path): 路径对象

        Returns:
            str: 去除文件扩展名后的文件名称
        """
        # 强制转换为Path对象
        if not isinstance(path, Path):
            path = Path(path)
        # 获取文件名称
        filename = path.name
        # 获取文件所有后缀名
        suffixes = path.suffixes
        if suffixes:
            for suffix in suffixes:
                filename = filename.replace(suffix, "")
        return filename
